require.config({
	paths:{
		'jquery':'jquery',
		'cookie':'jquery.cookie'
	}
})
require(['jquery','cookie'],function($,cookie){
	$(function(){
		//取出存储在cookie中的购物车信息
		var cartStr = $.cookie('cart') ? $.cookie('cart') : '';
		if(!cartStr){
			$('.blank').css('display','block');
		}else{
			var cartObj = convertCartStrToObj(cartStr);
			for(var id in cartObj){
				var good = cartObj[id];
				//console.log(good,id);
				var str = '<ul class="goodInfo" data-good-id="' + id + '"> ' + 
				'<li><img src="' + good.src + '" /></li>' +
				'<li>' + good.name + '</li>' +
				'<li>' + good.price + '</li>' + 
				'<li class="num">' + 
					'<a href="javascript:;" class="minus">-</a>' + 
					'<input type="text" name="" id="" value="' + good.num + '" />' +
					'<a href="javascript:;" class="plus">+</a>' +
				'</li>' +
				'<li class="total">' + good.num * good.price + '</li>' + 
				'<li><a href="javascript:;" class="del">删除</a></li>' +
			'</ul>'
			$(str).appendTo($('.cartList'));
			}
			//给每个删除按钮添加点击事件
			$('.goodInfo .del').click(function(){
				var id = $(this).parents('.goodInfo').remove().attr('data-good-id');
				//console.log(id);
				var cartStr = $.cookie('cart') ? $.cookie('cart') : '';
				var cartObj = convertCartStrToObj(cartStr);
				delete cartObj[id];
				$.cookie('cart',convertObjToCartStr(cartObj),{expires:7,path:'/'})
			})
			//给增加按钮添加事件
			$('.plus').click(function(){
				var id = $(this).parents('.goodInfo').attr('data-good-id');
				var cartStr = $.cookie('cart') ? $.cookie('cart') : '';
				var cartObj = convertCartStrToObj(cartStr);
				cartObj[id].num ++;
				//页面上显示的数据加1
				$(this).siblings('input').val('' + cartObj[id].num);
				//更新小计
				$(this).parents('.num').siblings('.total').html(cartObj[id].price * cartObj[id].num + '');
				$.cookie('cart',convertObjToCartStr(cartObj),{expires:7,path:'/'});
			})
			//给减少按钮添加事件
			$('.minus').click(function(){
				var id = $(this).parents('.goodInfo').attr('data-good-id');
				var cartStr = $.cookie('cart') ? $.cookie('cart') : '';
				var cartObj = convertCartStrToObj(cartStr);
				if(cartObj[id].num > 1){
					cartObj[id].num --;
					//页面上显示的数据加1
					$(this).siblings('input').val('' + cartObj[id].num);
					//更新小计
					$(this).parents('.num').siblings('.total').html(cartObj[id].price * cartObj[id].num + '');
					$.cookie('cart',convertObjToCartStr(cartObj),{expires:7,path:'/'});
				}				
			})
			//文本框失焦事件
			$('.goodInfo .num input').blur(function(){
				var id = $(this).parents('.goodInfo').attr('data-good-id');
				var cartStr = $.cookie('cart') ? $.cookie('cart') : '';
				var cartObj = convertCartStrToObj(cartStr);
				//判断输入是否合法
				var re = /^\d+$/;
				if(re.test($(this).val()) == false){
					cartObj[id].num = 1;
					$(this).val('1');
				}else{
					cartObj[id].num = parseInt($(this).val());
				}
				$(this).parents('.num').siblings('.total').html(cartObj[id].price * cartObj[id].num + '');
				$.cookie('cart',convertObjToCartStr(cartObj),{expires:7,path:'/'});
			})
		}
		
		//将字符串转换为对象
		function convertCartStrToObj(cartStr){
			if(!cartStr){
				return {};
			}
			var goods = cartStr.split(':');
			var obj = {};
			for(var i = 0;i < goods.length;i ++){
				var data = goods[i].split(',');
				obj[data[0]] = {
					name : data[1],
					price: parseFloat(data[2]),
					num: parseInt(data[3]),
					src: data[4]
				}
			}
			return obj;
		}
		//将对象转换为字符串
		function convertObjToCartStr(obj){
			var cartStr = '';
			for(var id in obj){
				if(cartStr){
					cartStr += ':';
				}
				cartStr += id + ',' + obj[id].name + ',' + obj[id].price + ',' + obj[id].num + ',' + obj[id].src;
			}
			return cartStr;
		}		
	})
})